package com.xqb.modules.controller.course;

import com.xqb.common.annotation.Anonymous;
import com.xqb.common.annotation.Log;
import com.xqb.common.core.controller.BaseController;
import com.xqb.common.core.domain.AjaxResult;
import com.xqb.common.core.page.TableDataInfo;
import com.xqb.common.enums.BusinessType;
import com.xqb.common.utils.poi.ExcelUtil;
import com.xqb.modules.domain.dto.LesCatalogueListDto;
import com.xqb.modules.domain.po.LesCourseCatalogue;
import com.xqb.modules.domain.vo.LesAllCatalogueVo;
import com.xqb.modules.service.ILesCourseCatalogueService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程目录Controller
 * 
 * @author xqb
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/course/catalogue")
public class LesCourseCatalogueController extends BaseController
{
    @Autowired
    private ILesCourseCatalogueService lesCourseCatalogueService;

    /**
     * 查询课程目录列表
     */
    @PreAuthorize("@ss.hasPermi('course:catalogue:list')")
    @GetMapping("/list")
    public TableDataInfo list(LesCourseCatalogue lesCourseCatalogue)
    {
        startPage();
        List<LesCourseCatalogue> list = lesCourseCatalogueService.selectLesCourseCatalogueList(lesCourseCatalogue);
        return getDataTable(list);
    }

    /**
     * 导出课程目录列表
     */
    @PreAuthorize("@ss.hasPermi('course:catalogue:export')")
    @Log(title = "课程目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LesCourseCatalogue lesCourseCatalogue)
    {
        List<LesCourseCatalogue> list = lesCourseCatalogueService.selectLesCourseCatalogueList(lesCourseCatalogue);
        ExcelUtil<LesCourseCatalogue> util = new ExcelUtil<LesCourseCatalogue>(LesCourseCatalogue.class);
        util.exportExcel(response, list, "课程目录数据");
    }

    /**
     * 获取课程目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:catalogue:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lesCourseCatalogueService.selectLesCourseCatalogueById(id));
    }

    /**
     * 新增课程目录
     */
    @PreAuthorize("@ss.hasPermi('course:catalogue:add')")
    @Log(title = "课程目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LesCourseCatalogue lesCourseCatalogue)
    {
        return toAjax(lesCourseCatalogueService.insertLesCourseCatalogue(lesCourseCatalogue));
    }

    /**
     * 修改课程目录
     */
    @PreAuthorize("@ss.hasPermi('course:catalogue:edit')")
    @Log(title = "课程目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LesCourseCatalogue lesCourseCatalogue)
    {
        return toAjax(lesCourseCatalogueService.updateLesCourseCatalogue(lesCourseCatalogue));
    }

    /**
     * 删除课程目录
     */
    @PreAuthorize("@ss.hasPermi('course:catalogue:remove')")
    @Log(title = "课程目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lesCourseCatalogueService.deleteLesCourseCatalogueByIds(ids));
    }
    /**
     * 批量增加目录
     */
    @ApiOperation(value = "批量增加目录")
    @PreAuthorize("@ss.hasPermi('course:catalogue:add')")
    @Log(title = "目录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addList")
    public AjaxResult addList(@RequestBody List<LesCourseCatalogue> lesCourseCatalogueList)
    {
        return toAjax(lesCourseCatalogueService.insertLesCourseCatalogueList(lesCourseCatalogueList));
    }
    /**
     * 获取目录章节
     */
    @ApiOperation(value = "获取目录章节")
    @PreAuthorize("@ss.hasPermi('course:catalogue:list')")
    @GetMapping(value = "/chapters/{courseId}")
    public AjaxResult getChapterList(@PathVariable("courseId") Long courseId)
    {
        return success(lesCourseCatalogueService.selectCourseChapterList(courseId));
    }
    /**
     * 获取章节下的小节
     */
    @ApiOperation(value = "获取章节下的小节")
    @PreAuthorize("@ss.hasPermi('course:catalogue:list')")
    @GetMapping(value = "/subsection/{chapterId}")
    public AjaxResult getSubsectionList(@PathVariable("chapterId") Long id)
    {
        return success(lesCourseCatalogueService.selectSubsectionList(id));
    }
    /**
     * 查询所有章节及对应的小节
     */
    @ApiOperation(value = "查询所有章节及对应的小节")
    @Anonymous
    @GetMapping("/list/{courseId}")
    public AjaxResult list(@PathVariable("courseId") Long courseId)
    {
        List<LesAllCatalogueVo> list = lesCourseCatalogueService.selectAllCatalogueList(courseId);
        return AjaxResult.success(list);
    }
    /**
     * 增加章节及对应小节
     */
    @ApiOperation(value = "增加章节及对应小节")
    @PreAuthorize("@ss.hasPermi('course:catalogue:add')")
    @Log(title = "目录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addChapterList")
    public AjaxResult addChapterList(@RequestBody List<LesCatalogueListDto> lesCourseCatalogueList)
    {
        return toAjax(lesCourseCatalogueService.insertAllCatalogueList(lesCourseCatalogueList));
    }
}
